package cn.sdack.go.users.controller;

import cn.sdack.go.common.dtos.users.UserDTO;
import cn.sdack.go.common.entities.JsonResult;
import cn.sdack.go.common.entities.users.UserEntity;
import cn.sdack.go.users.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.oauth2.server.resource.authentication.JwtAuthenticationToken;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author sdack
 * @date 2023/12/26
 */
@RestController
public class UserController {

    @Autowired
    private UserService userService;


    @GetMapping("/index")
    public JsonResult<?> index() {
        List<String> list = List.of("5", "4", "3");
        return JsonResult.toJson(list);
    }

    @GetMapping("/user/info")
    public JsonResult<?> info(@RequestParam(value = "a",defaultValue = "") String account) {
        try {
            if (!StringUtils.hasLength(account)) {
                JwtAuthenticationToken authentication = (JwtAuthenticationToken) SecurityContextHolder.getContext().getAuthentication();
                account = authentication.getToken().getClaim("account");
            }
            UserEntity userinfo = userService.userinfo(account);
            return JsonResult.toJson(userinfo);
        } catch (Exception e) {
            String message = e.getMessage().toString();
            if (message.length() > 150) {
                message = message.substring(0, 150);
            }
            return JsonResult.toJson(false, message);
        }
    }

    @PostMapping("/user")
    public JsonResult<?> add(@RequestBody UserDTO param) {
        if (param.getEmail() == null && param.getPhone() == null) {
            return JsonResult.toJson(false, "邮箱手机号必选其一");
        }
        if (!StringUtils.hasLength(param.getPwd())) {
            return JsonResult.toJson(false, "密码必填");
        }
        try {
            UserEntity userEntity = userService.add(param);
            return JsonResult.toJson(userEntity);
        } catch (Exception e) {
            String message = e.getMessage().toString();
            if (message.length() > 150) {
                message = message.substring(0, 150);
            }
            return JsonResult.toJson(false, message);
        }
    }

    /**
     * 编辑用户基本信息
     * @param param
     * @return
     */
    @PostMapping("/user/userinfo")
    public JsonResult<?> editUserinfo(@RequestBody UserDTO param) {
        try {
            UserEntity userEntity = userService.editUserinfo(param);
            return JsonResult.toJson(userEntity);
        } catch (Exception e) {
            String message = e.getMessage().toString();
            if (message.length() > 150) {
                message = message.substring(0, 150);
            }
            return JsonResult.toJson(false, message);
        }
    }

    /**
     * 设置用户身份
     * @param uid
     * @param role
     * @return
     */
    @GetMapping("/user/setIdentity")
    public JsonResult<?> setIdentity(@RequestParam("u") Long uid,@RequestParam("i") String role) {
        try {
            userService.setIdentity(uid,role);
            return JsonResult.toJson(true);
        } catch (Exception e) {
            String message = e.getMessage().toString();
            if (message.length() > 150) {
                message = message.substring(0, 150);
            }
            return JsonResult.toJson(false, message);
        }
    }

    @GetMapping("/user/state")
    public JsonResult<?> setState(@RequestParam("u") Long uid,@RequestParam("state") Integer state) {
        try {
            userService.getById(uid);
            userService.setState(uid,state);
            return JsonResult.toJson(true);
        } catch (Exception e) {
            String message = e.getMessage().toString();
            if (message.length() > 150) {
                message = message.substring(0, 150);
            }
            return JsonResult.toJson(false, message);
        }
    }

}
